﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///管理日志表
    /// </summary>
    public partial class manager_log
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig(); //获得系统配置信息
        private readonly DAL.Data data;

        public manager_log()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_manager_log>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_manager_log model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_manager_log model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除7天前的日志数据
        /// </summary>
        public int Delete(int dayCount)
        {
            return data.Delete<Entitys.dt_manager_log>(x => x.add_time < DateTime.Now.AddDays(-dayCount));
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_manager_log GetModel(int id)
        {
            return data.Get<Entitys.dt_manager_log>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_manager_log> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_manager_log>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_manager_log> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_manager_log>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================

        /// <summary>
        /// 增加管理日志
        /// </summary>
        /// <param name="用户id"></param>
        /// <param name="用户名"></param>
        /// <param name="操作类型"></param>
        /// <param name="备注"></param>
        /// <returns></returns>
        public int Add(int user_id, string user_name, string action_type, string remark)
        {
            Entitys.dt_manager_log manager_log_model = new Entitys.dt_manager_log();
            manager_log_model.user_id = user_id;
            manager_log_model.user_name = user_name;
            manager_log_model.action_type = action_type;
            manager_log_model.remark = remark;
            manager_log_model.user_ip = DTRequest.GetIP();
            return data.Insert(manager_log_model);
        }

        /// <summary>
        /// 根据用户名返回上一次登录记录
        /// </summary>
        public Entitys.dt_manager_log GetModel(string user_name, int top_num, string action_type)
        {
            return data.Get<Entitys.dt_manager_log>(x => x.user_name == user_name && x.action_type == action_type, x => x.id, SqlSugar.OrderByType.Desc);
        }
        #endregion
    }
}